//
//  PlatformMapView.swift
//  amap
//
//  Created by James Karl on 2019/9/11.
//

import Foundation
import MAMapKit

class PlatformMapView : NSObject, FlutterPlatformView {
    
    static var annotationFactory: MapAnnotationFactory? = nil
    
    private let registrar: FlutterPluginRegistrar
    private let frame: CGRect
    private let viewId: Int
    private let creationParams: MapCreationParams?
    
    private let mapView: MAMapView
    private let messageHandler: MapMessageHandler
    
    init(withFrame frame: CGRect, viewIdentifier viewId: Int, creationParams: MapCreationParams?, registrar: FlutterPluginRegistrar) {
        self.frame = frame
        self.viewId = viewId
        self.creationParams = creationParams
        self.registrar = registrar
        
        mapView = MAMapView(frame: frame)
        
        messageHandler = MapMessageHandler(registrar: registrar, mapView: mapView, viewId: viewId)
        messageHandler.setup()
        
    }
    
    public func view() -> UIView {
        return mapView
    }
    
    func initWithCreationParams() {
        if let cameraArgs = creationParams?.cameraPosition {
            if let centerPoint = cameraArgs.target {
                mapView.setCenter(centerPoint.toMapPoint(), animated: false)
            }
            if let zoom = cameraArgs.zoom {
                mapView.setZoomLevel(CGFloat(zoom), animated: false)
            }
            
            if let minZoom = cameraArgs.minZoom {
                print("minZoom \(minZoom)")
                mapView.minZoomLevel = CGFloat(minZoom)
            }
            
            if let maxZoom = cameraArgs.maxZoom {
                print("maxZoom \(maxZoom)")
                mapView.maxZoomLevel = CGFloat(maxZoom)
            }
        }
        
        if let settings = creationParams?.settings {
            if let s = settings.myLocationButtonEnabled {
                mapView.showsUserLocation = s
                mapView.userTrackingMode = .follow
            }
            
            if let s = settings.mapLogoPosition {
                print("logo position \(s)")
                //TODO set logo position
            }
            
            if let s = settings.compassEnabled {
                mapView.showsCompass = s
            }
            
            if let s = settings.gestureScaleByMapCenter {
                print("gestureScaleByMapCenter \(s)")
                //TODO gestureScaleByMapCenter
            }
            
            if let s = settings.indoorSwitchEnabled {
                mapView.isShowsIndoorMapControl = s
            }
            
            if let s = settings.zoomControlsEnabled {
                print("zoomControlsEnabled \(s)")
            }
            
            if let s = settings.rotateGesturesEnabled {
                mapView.isRotateEnabled = s
                mapView.isRotateCameraEnabled = s
            }
            
            if let s = settings.scaleControlsEnabled {
                mapView.showsScale = s
            }
            
        }
    }
}
